home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.3 KB | 95 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 5.1 (Least Squares Line).
- % Section 5.1, Least-Squares Line, Page 264
- echo on; clc; format short; hold off; clear
-
- % This program finds the least squares line, given a set of
-
- % data points { (x , y ), (x , y ) ,..., (x , y ) }.
- % 1 1 2 2 n n
-
- % The abscissas and ordinates are stored in X and Y, respectively.
-
- % X = [x , x ,..., x ]; Y = [y , y ,..., y ];
- % 1 2 n 1 2 n
-
- % Remark. lsline.m is used for Algorithm 5.1
-
- pause % Press any key to continue.
-
- clc;
- % Place the abscissas for the points in X.
-
- % Place the ordinates for the points in Y.
-
- X = [-1 0 1 2 3 4 5 6];
-
- Y = [10 9 7 5 4 3 0 -1];
-
- [A B] = lsline(X,Y);
-
- pause % Press any key to graph data points.
-
- clc; clg;
- a = min(X)-0.20; b = max(X)+0.20;
- c = min(Y)-0.35; d = max(Y)+0.35;
- axis([a b c d]);...
- plot(X,Y,'or');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('The given x-y data points.');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- points = [X;Y]; format short;
- clc,disp(''),disp('The given x-y data points.'),...
- disp(' x y'),disp(points'),...
-
- pause % Press any key to continue.
-
- clc; clg;
- a = -1;
- b = 7;
- c = -2;
- d = 12;
- Xs = [a b];
- Ys = A*Xs + B;
- axis([a b c d]);...
- plot(X,Y,'or',Xs,Ys,'-g');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- Mx1 = 'Least squares line: f(x) = ';...
- Mx2 = [Mx1,num2str(A),' x'];...
- if B > 0,
- Mx3 = [Mx2,' + ',num2str(B)];
- else
- Mx3 = [Mx2,' - ',num2str(abs(B))];
- end;...
- title(Mx3);...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- Mx4 = 'The given x-y data points.';
- clc,echo off,diary output,...
- disp(''),disp(Mx3),disp(Mx4),...
- disp(' x y'),disp(points'),diary off, echo on
- pause % Press any key to continue.
- points = [X;Y;A*X+B;Y-(A*X+B)]';
- Mx5=' x(k) y(k) f(x(k)) error';
- clc,echo off,diary output,disp(''),disp(Mx3),...
- disp(''),disp(Mx5),disp(points),diary off,echo on
-